package com.car.loan.project.cm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmBank;
import com.car.loan.project.cm.domain.CmBankCarType;
import com.car.loan.project.cm.domain.CmBankScheme;
import com.car.loan.project.cm.service.ICmBankCarTypeService;
import com.car.loan.project.cm.service.ICmBankSchemeService;
import com.car.loan.project.cm.service.impl.CmBankServiceImpl;
import com.car.loan.project.cm.util.bo.BankAddBo;
import com.car.loan.project.cm.util.bo.BankUpdateBo;
import com.car.loan.project.cm.util.select.BankBaseSelect;
import com.car.loan.project.cm.util.vo.BankCarTypeInfoVo;
import com.car.loan.project.cm.util.vo.BankInfoVo;
import com.car.loan.project.cm.util.vo.BankSchemeInfo;
import com.car.loan.project.xr.util.RedisDataUtil;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/10/22 20:08
 */
@RestController
@RequestMapping("/cm/bank")
@Api(value = "银行管理", tags = "基础数据-银行管理接口")
public class CmBankController {

    @Autowired
    private CmBankServiceImpl bankService;

    @Autowired
    private ICmBankSchemeService bankSchemeService;

    @Autowired
    private ICmBankCarTypeService bankCarTypeService;

    @Resource
    private RedisDataUtil redisDataUtil;


    @PreAuthorize("@ss.hasPermi('cm:bank:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取银行列表", responseContainer = "list", response = CmBank.class)
    public AjaxResult list(@Valid BankBaseSelect bankBaseSelect) {
        IPage<CmBank> page = new Page<>(bankBaseSelect.getPageNum(),
            bankBaseSelect.getPageSize());
        QueryWrapper<CmBank> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bankBaseSelect.getName())) {
            queryWrapper.like("name", bankBaseSelect.getName());
        }
        return AjaxResult.success(bankService.selectPage(page, queryWrapper));
    }

    @PreAuthorize("@ss.hasPermi('cm:bank:query')")
    @GetMapping(value = "/{bankId}")
    @ApiOperation(value = "获取银行详情", response = BankInfoVo.class)
    public AjaxResult getInfo(@PathVariable Long bankId) {
        CmBank cmBank = bankService.selectCmBankById(bankId);
        if (null != cmBank) {
            BankInfoVo bankInfoVo = new BankInfoVo();
            BeanUtils.copyBeanProp(bankInfoVo, cmBank);
            //银行车型
            CmBankCarType cmBankCarTypeQuery = new CmBankCarType();
            cmBankCarTypeQuery.setBankId(bankId);
            List<CmBankCarType> cmBankCarTypes = bankCarTypeService
                .selectCmBankCarTypeList(cmBankCarTypeQuery);

            if (StringUtils.isNotEmpty(cmBankCarTypes)) {
                //查询方案
                CmBankScheme cmBankSchemeQuery = new CmBankScheme();
                cmBankSchemeQuery.setBankId(bankId);
                List<CmBankScheme> cmBankSchemes = bankSchemeService
                    .selectCmBankSchemeList(cmBankSchemeQuery);
                if (StringUtils.isEmpty(cmBankSchemes)) {
                    cmBankSchemes = Lists.newArrayList();
                }
                //方案分组
                Map<Long, List<CmBankScheme>> cmBankSchemeMap = cmBankSchemes.stream()
                    .collect(Collectors.groupingBy(CmBankScheme::getBankCarTypeId));
                List<BankCarTypeInfoVo> bankCarTypeInfoVos = Lists.newArrayList();
                for (CmBankCarType cmBankCarType : cmBankCarTypes) {
                    BankCarTypeInfoVo bankCarTypeInfoVo = new BankCarTypeInfoVo();
                    BeanUtils.copyBeanProp(bankCarTypeInfoVo, cmBankCarType);
                    //获取车型对应方案
                    List<CmBankScheme> cmBankSchemesByCarType = cmBankSchemeMap
                        .get(cmBankCarType.getId());
                    if (StringUtils.isNotEmpty(cmBankSchemesByCarType)) {
                        List<BankSchemeInfo> bankSchemeInfoList = Lists.newArrayList();
                        for (CmBankScheme cmBankScheme : cmBankSchemesByCarType) {
                            BankSchemeInfo bankSchemeInfo = new BankSchemeInfo();
                            BeanUtils.copyBeanProp(bankSchemeInfo, cmBankScheme);
                            bankSchemeInfoList.add(bankSchemeInfo);

                        }
                        bankCarTypeInfoVo.setBankSchemeInfoList(bankSchemeInfoList);
                    }
                    bankCarTypeInfoVos.add(bankCarTypeInfoVo);
                }
                bankInfoVo.setBankCarTypeInfoVos(bankCarTypeInfoVos);
            }
            return AjaxResult.success(bankInfoVo);
        }
        return AjaxResult.error("不存在该银行信息");
    }


    @PreAuthorize("@ss.hasPermi('cm:bank:add')")
    @Log(title = "银行管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加银行")
    public AjaxResult add(@RequestBody @Valid BankAddBo bankAddBo) {
        bankService.insertCmBank(bankAddBo);
        redisDataUtil.reloadBankRedis();
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('cm:bank:edit')")
    @Log(title = "银行管理", businessType = BusinessType.INSERT)
    @PutMapping
    @ApiOperation("修改银行信息")
    public AjaxResult edit(@RequestBody @Valid BankUpdateBo bankUpdateBo) {
        bankService.updateCmBank(bankUpdateBo);
        redisDataUtil.reloadBankRedis();
        return AjaxResult.success();
    }

}
